package com.riven.redisson.concurrent;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author riven
 * @since 2025-05-22
 */
@Slf4j
public class LockSubjectResolverComposite implements LockSubjectResolver {
    private final List<LockSubjectResolver> resolvers = new ArrayList<>();

    @Override
    public Object resolve(ProceedingJoinPoint joinPoint) {
        for (LockSubjectResolver resolver : resolvers) {
            Object resolved = resolver.resolve(joinPoint);
            if (resolved == DO_NOT_LOCK) {
                return null;
            }
            if (resolved != null) {
                return resolved;
            }
        }
        return null;
    }

    public void addResolvers(LockSubjectResolver... resolvers) {
        this.resolvers.addAll(Arrays.asList(resolvers));
    }
}
